<template>
  <div class="course-list-container">
    <div class="filters">
      <div class="search-bar">
        <input
          type="text"
          v-model="searchQuery"
          placeholder="搜索课程..."
          @input="handleSearch"
        />
      </div>
      <div class="filter-options">
        <select v-model="selectedCategory" @change="handleFilter">
          <option value="">所有分类</option>
          <option v-for="category in categories" :key="category.id" :value="category.id">
            {{ category.name }}
          </option>
        </select>
        <select v-model="selectedLevel" @change="handleFilter">
          <option value="">所有难度</option>
          <option value="beginner">入门</option>
          <option value="intermediate">中级</option>
          <option value="advanced">高级</option>
        </select>
      </div>
    </div>

    <div class="courses-grid" v-if="courses.length">
      <div v-for="course in courses" :key="course.id" class="course-card">
        <img :src="course.coverImage" :alt="course.title" class="course-image">
        <div class="course-info">
          <h3>{{ course.title }}</h3>
          <p class="description">{{ course.description }}</p>
          <div class="course-meta">
            <span class="instructor">讲师: {{ course.instructor }}</span>
            <span class="level">难度: {{ getLevelText(course.level) }}</span>
          </div>
          <div class="course-stats">
            <span><i class="fas fa-users"></i> {{ course.studentCount }}人学习</span>
            <span><i class="fas fa-star"></i> {{ course.rating }}分</span>
          </div>
          <div class="course-price">
            <span class="price">¥{{ course.price }}</span>
            <router-link :to="`/courses/${course.id}`" class="enroll-btn">查看详情</router-link>
          </div>
        </div>
      </div>
    </div>

    <div v-else class="no-courses">
      <p>暂无符合条件的课程</p>
    </div>

    <div class="pagination" v-if="totalPages > 1">
      <button
        :disabled="currentPage === 1"
        @click="changePage(currentPage - 1)"
      >
        上一页
      </button>
      <span>{{ currentPage }} / {{ totalPages }}</span>
      <button
        :disabled="currentPage === totalPages"
        @click="changePage(currentPage + 1)"
      >
        下一页
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useCourseStore } from '../../stores/course.store'

const courseStore = useCourseStore()

const courses = ref([])
const categories = ref([])
const searchQuery = ref('')
const selectedCategory = ref('')
const selectedLevel = ref('')
const currentPage = ref(1)
const totalPages = ref(1)

const getLevelText = (level: string) => {
  const levelMap = {
    beginner: '入门',
    intermediate: '中级',
    advanced: '高级'
  }
  return levelMap[level] || level
}

const loadCourses = async () => {
  try {
    const result = await courseStore.getCourses({
      page: currentPage.value,
      category: selectedCategory.value,
      level: selectedLevel.value,
      search: searchQuery.value
    })
    courses.value = result.courses
    totalPages.value = result.totalPages
  } catch (error) {
    console.error('Failed to load courses:', error)
  }
}

const loadCategories = async () => {
  try {
    categories.value = await courseStore.getCategories()
  } catch (error) {
    console.error('Failed to load categories:', error)
  }
}

const handleSearch = () => {
  currentPage.value = 1
  loadCourses()
}

const handleFilter = () => {
  currentPage.value = 1
  loadCourses()
}

const changePage = (page: number) => {
  currentPage.value = page
  loadCourses()
}

onMounted(() => {
  loadCategories()
  loadCourses()
})
</script>

<style scoped>
.course-list-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.filters {
  margin-bottom: 2rem;
}

.search-bar {
  margin-bottom: 1rem;
}

.search-bar input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

.filter-options {
  display: flex;
  gap: 1rem;
}

.filter-options select {
  padding: 0.5rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  min-width: 150px;
}

.courses-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 2rem;
}

.course-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
}

.course-card:hover {
  transform: translateY(-5px);
}

.course-image {
  width: 100%;
  height: 180px;
  object-fit: cover;
}

.course-info {
  padding: 1.5rem;
}

.course-info h3 {
  font-size: 1.2rem;
  margin-bottom: 0.5rem;
  color: #333;
}

.description {
  color: #666;
  margin-bottom: 1rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.course-meta {
  display: flex;
  justify-content: space-between;
  color: #666;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.course-stats {
  display: flex;
  justify-content: space-between;
  color: #666;
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

.course-price {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  font-size: 1.2rem;
  color: #e74c3c;
  font-weight: bold;
}

.enroll-btn {
  padding: 0.5rem 1rem;
  background-color: #4CAF50;
  color: white;
  text-decoration: none;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.enroll-btn:hover {
  background-color: #45a049;
}

.no-courses {
  text-align: center;
  padding: 2rem;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 2rem;
  gap: 1rem;
}

.pagination button {
  padding: 0.5rem 1rem;
  border: 1px solid #ddd;
  background-color: white;
  border-radius: 4px;
  cursor: pointer;
}

.pagination button:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}

.pagination span {
  color: #666;
}
</style>